﻿function AzureOnlineQueuesDataContext(context) {
    var self = this;

    var queue = ko.observable('');
    var queues = ko.observableArray([]);
    var messages = ko.observableArray([]);

    var queuesSettings = ko.computed(function () {
        var as = context.accountSettings();
        return as.folder('queues');
    });

    function addNewQueue(que) {
        var ques = queues();
        ques.push(que);
        ques.sort(function (a, b) {
            var an = a.name();
            var bn = b.name();
            if (an > bn) return 1;
            if (an < bn) return -1;
            return 0;
        });
        queues(ques);
    }

    function findByName(name) {
        var items = queues();
        for (var i = 0; i < items.length; i++) {
            var item = items[i];
            if (item.name() == name) return item;
        }
        return null;
    }

    function removeQueue(que) {
        queues.remove(que);
    }

    self.queue = queue;
    self.queues = queues;
    self.queuesSettings = queuesSettings;
    self.messages = messages;
    self.addNewQueue = addNewQueue;
    self.findByName = findByName;
    self.removeQueue = removeQueue;
}